// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')

const del = require('del')

const browserSync = require('browser-sync')
const bs = browserSync.create()

const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

const sass = require('gulp-sass')(require('sass'))
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')

// 模板里面用到的data数据
const data = {
    menus: [
        {
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [
                {
                    name: 'Twitter',
                    link: 'https://twitter.com/w_zce'
                },
                {
                    name: 'About',
                    link: 'https://weibo.com/zceme'
                },
                {
                    name: 'divider'
                },
                {
                    name: 'About',
                    link: 'https://github.com/zce'
                }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

const clean = () => {
    return del(['dist', 'temp'])
}

const style = () => {
    return src('src/assets/styles/*.scss', { base: 'src' })
        .pipe(sass({ outputStyle: 'expanded' }))
        .pipe(dest('temp'))
}

const script = () => {
    return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
        .pipe(dest('temp'))
}

const page = () => {
    return src('src/*.html', { base: 'src' })
        .pipe(plugins.swig({ data: data }))
        .pipe(dest('temp'))
}

const image = () => {
    return src('src/assets/images/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

const font = () => {
    return src('src/assets/fonts/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

const extra = () => {
    return src('public/**', { base: 'public' })
        .pipe(dest('dist'))
}

const serve = () => {
    watch('src/assets/styles/*.scss', style)
    watch('src/assets/scripts/*.js', script)
    watch('src/*.html', page)
    // watch('src/assets/images/**', image)
    // watch('src/assets/fonts/**', font)
    // watch('public/**', extra)
    // 监听到里面的文件修改之后，就会执行相应的任务，任务一旦触发就会把dist文件覆盖掉，同时brower-sync监听到dist变化，自动同步到浏览器

    // 让图片、字体、public文件变化后可以更新浏览器，浏览器就会重新发起资源请求，就可以拿到变化后的文件
    watch([
        'src/assets/images/**',
        'src/assets/fonts/**',
        'public/**'
    ], bs.reload)

    bs.init({
        notify: false,              //设置每次启动预览时右上角的提示是否显示
        port: 2080,                 //端口 默认3000
        open: true,                 //启动时自动打开浏览器，默认true
        files: 'dist/**',           //启动后监听的文件
        server: {
            baseDir: ['temp', 'src', 'public'], //网站的根目录，当请求过来，找不到文件时，会依次向后找
            routes: {
                '/node_modules': 'node_modules'
            }
            //routes 优先于 baseDir，即请求发生以后，会先看 routes 里面有没有，否则走 baseDir 下面的目录
        }
    })
}


const useref = () => {
    return src('temp/*.html', { base: 'temp' })
        // 处理引用文件的合并，首先要先找到这些文件，searchPath用来指定从哪些文件路径查找，这里指定dist和根目录
        .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
        // 判断引用的文件的类型 html/css/js
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
            collapseWhitespace: true,   //折叠所有的空白字符和换行符
            minifyCSS: true,            //压缩html文件里style标签内的css代码
            minifyJS: true              //压缩html文件里script标签内的js代码
        })))
        // 在同一文件夹边读边写可能出现问题
        .pipe(dest('dist'))
}

const compile = parallel(style, script, page)

const build = series(
    clean, 
    parallel(
        series(compile, useref), //useref需要在compile之后执行
        image, 
        font, 
        extra
    )
)

const develop = series(compile, serve)


module.exports = {
    clean,
    build,
    develop
}